﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace MyNote.Helpers
{
    public class PagerSetting
    {
        public static readonly int RecordPerPage = 50;

        public static readonly int DefaultPageSize = 10;

        public int CurrentPage { get; set; }

        public int PageSize { get; set; }

        public int TotalRecords { get; set; }

    }

    public static class ViewHelpers
    {
        public static string Pager(this HtmlHelper helper, PagerSetting setting, string urlPrefix)
        {
            StringBuilder sb1 = new StringBuilder();

            int seed = setting.CurrentPage % setting.PageSize == 0 ? setting.CurrentPage : setting.CurrentPage - (setting.CurrentPage % setting.PageSize);

            if (setting.CurrentPage > 0)
                sb1.AppendLine(String.Format("<a href=\"{0}{1}\">上一页</a>", urlPrefix, setting.CurrentPage));

            if (setting.CurrentPage - setting.PageSize >= 0)
                sb1.AppendLine(String.Format("<a href=\"{0}{1}\">...</a>", urlPrefix, (setting.CurrentPage - setting.PageSize) + 1));

            for (int i = seed; i < Math.Round((setting.TotalRecords / PagerSetting.RecordPerPage) + 0.5) && i < seed + setting.PageSize; i++)
            {
                sb1.AppendLine(String.Format("<a href=\"{0}{1}\">{1}</a>", urlPrefix, i + 1));
            }

            if (setting.CurrentPage + setting.PageSize <= (Math.Round((setting.TotalRecords / PagerSetting.RecordPerPage) + 0.5) - 1))
                sb1.AppendLine(String.Format("<a href=\"{0}{1}\">...</a>", urlPrefix, (setting.CurrentPage + setting.PageSize) + 1));

            if (setting.CurrentPage < (Math.Round((setting.TotalRecords / PagerSetting.RecordPerPage) + 0.5) - 1))
                sb1.AppendLine(String.Format("<a href=\"{0}{1}\">下一页</a>", urlPrefix, setting.CurrentPage + 2));

            return sb1.ToString();
        }
    }
}